<template>
  <!-- 底部 -->
  <div class="my-footer">
    <!-- 全选 -->
    <div class="custom-control custom-checkbox">
      <!-- checked从totalEvey获取值，绑定点击事件返回状态给父组件 -->
      <input type="checkbox" class="custom-control-input" id="footerCheck" :checked="totalEvey" @click="$emit('check', $event.target.checked)">
      <label class="custom-control-label" for="footerCheck">全选</label>
    </div>
    <!-- 合计 -->
    <div>
      <span>合计:</span>
      <!-- 显示计算结果 -->
      <span class="price">¥ {{totalPrice}}</span>
    </div>
    <!-- 按钮 -->
    <!-- 通过新的total数组来计算几个选中 -->
    <button type="button" class="footer-btn btn btn-primary">结算 ( {{ total.length }} )</button>
  </div>
</template>

<script>
export default {
  // 接收list
  props:{
    list:{
      type: Array,
      required: true
    }
  },
  computed:{
    // 先吧选中的从数组中取出
    total(){
      return this.list.filter(item => item.goods_state)
    },
    // 通过新数组计算总价，并保留两位小数
    totalPrice(){
      return this.total.reduce((sum,item)=> sum + (item.goods_price * item.goods_count),0).toFixed(2)
    },
    // 通过every来判断受否全都是true并返回值
    totalEvey(){
      return this.list.every(item => item.goods_state)
    }
  }
}
</script>

<style lang="less" scoped>
.my-footer {
  position: fixed;
  z-index: 2;
  bottom: 0;
  width: 100%;
  height: 50px;
  border-top: 1px solid #ccc;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
  background: #fff;

  .price {
    color: red;
    font-weight: bold;
    font-size: 15px;
  }
  .footer-btn {
    min-width: 80px;
    height: 30px;
    line-height: 30px;
    border-radius: 25px;
    padding: 0;
  }
}
</style>